//
//  SRSportDetailViewController.m
//  SportRing
//
//  Created by elwin on 10/29/14.
//  Copyright (c) 2014 HotOne. All rights reserved.
//

#import "SRSportDetailViewController.h"
#import "SRSystemNetService.h"
#import "SRSportNetService.h"
#import "SRDay+Cate.h"
#import "SRMe+Cate.h"
#import "SRCurrent+Cate.h"
#import "SRSportPart+Cate.h"

@interface SRSportDetailViewController ()

@property (nonatomic, weak)IBOutlet SRSportDetailView *srSportDetailView;
@property (nonatomic, strong)NSArray *detail;
@property (nonatomic, strong)SRSportHeaderViewModel *header;


@end

@implementation SRSportDetailViewController

- (void)getSportDataForDay:(NSDate *)date
{
    NSString *dayStr = [NSDate stringFromDate:date format:@"yyyy-MM-dd"];
    
    [SVProgressHUD show];
    
    [[[SRSportNetService getTaskByDay:dayStr] flattenMap:^RACStream *(id value) {
        
        return [SRSportNetService getDaySportWithDate:dayStr];
        
    }] subscribeNext:^(id x) {
        
        [SVProgressHUD dismiss];
        
        SRDay * srDay = [SRDay findDayForDate:date forMe:[SRMe currentUser]];
        
        SRSportHeaderViewModel *headerModel = [[SRSportHeaderViewModel alloc] init];
        if ([_date isToday])
        {
            SRCurrent * current = [SRCurrent findCurrentForMe:[SRMe currentUser]];
            headerModel.averageTemperature =  [NSString stringWithFormat:@"%.1f", [current averageTemperature]];
            headerModel.EGL = [NSString stringWithFormat:@"%.1f", [current altitude]];
        }
        else
        {
            headerModel.averageTemperature =  [NSString stringWithFormat:@"%.1f", [srDay averageTemperature]];
            headerModel.EGL = [NSString stringWithFormat:@"%.1f", [srDay altitude]];
        }
        
        NSMutableArray *sportDetails = [[NSMutableArray alloc] init];
        
        NSArray * array = [SRSportPart findSportPartsForDay:srDay];
        
        if ([array count] > 0)
        {
            [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                SRSportPart *sportPart = (SRSportPart *)obj;
                
                SRSportDetailTableViewCellModel *cellModel = [[SRSportDetailTableViewCellModel alloc] init];
                
                cellModel.section = [sportPart.section intValue];
                cellModel.steps = [sportPart.runCounter intValue] + [sportPart.upstairsCounter intValue] +[sportPart.walkCounter intValue];
                cellModel.calorie = [sportPart.calorie floatValue];
                cellModel.distance = [sportPart.mileage floatValue] / 5000000;
                
                [sportDetails addObject:cellModel];
                
            }];
        }
        
        self.header = headerModel;
        self.detail = sportDetails;
        
        [_srSportDetailView SRSportDetailViewDataWithSportHeaderModel:_header sportArray:_detail];

        
    } error:^(NSError *error) {
        
        [SVProgressHUD showErrorWithStatus:@"获取数据失败"];
    }];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self addBackButton];
    
    [self getSportDataForDay:_date];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self hideTabbar];
}

@end
